package com.panghu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.panghu.entity.ApiResult;
import com.panghu.entity.Login;
import com.panghu.entity.Student;
import com.panghu.service.LoginService;
import com.panghu.service.StudentService;
import com.panghu.utils.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class RegisterController {

    @Autowired
    private StudentService studentService;

    @PostMapping("/register")
    public ApiResult registerStudent(@RequestBody Login register){
        //首先查询学号是否已经存在
        Integer userName = register.getUsername();

        //查询该学号是否存在
        Student stu = studentService.findStuById(userName);

        //如果存在该学号
        if(stu != null){
            return ApiResultHandler.buildApiResult(500,"用户已存在",null);
        }

        //用于保存数据的学生
        Student save = new Student();
        //保存学号
        save.setStudentId(register.getUsername());
        //保存密码
        save.setPwd(register.getPassword());
        //保存姓名
        save.setStudentName(register.getStuname());

        studentService.save(save);
        //保存该学生
        return ApiResultHandler.success(save);


    }
}
